﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using Evento.Models;

namespace Evento.Controllers
{ 
    public class EventsController : Controller
    {
		#region Fields 

        private readonly EventoDB _eventoDB;

		#endregion Fields 

		#region Constructors 

        /// <summary>
        /// Initializes a new instance of the <see cref="EventsController"/> class.
        /// </summary>
        public EventsController()
        {
            _eventoDB = new EventoDB();
        }

		#endregion Constructors 

		#region Methods 

        /// <summary>
        /// GET: /Events/Create 
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Create()
        {
            return View();
        }


        /// <summary>
        /// POST: /Events/Create
        /// </summary>
        /// <param name="event"></param>
        /// <returns></returns>
        [HttpPost, Authorize]
        public ActionResult Create(Event @event)
        {
            if (ModelState.IsValid)
            {
                _eventoDB.Events.Add(@event);
                _eventoDB.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(@event);
        }


        /// <summary>
        /// GET: /Events/Delete/id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(int id)
        {
            var @event = _eventoDB.Events.Find(id);
            return View(@event);
        }


        /// <summary>
        /// POST: /Events/Delete/id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var @event = _eventoDB.Events.Find(id);
            _eventoDB.Events.Remove(@event);
            _eventoDB.SaveChanges();
            return RedirectToAction("Index");
        }

        
        /// <summary>
        /// GET: /Events/Details/id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ViewResult Details(int id)
        {
            var @event = _eventoDB.Events.Find(id);
            return View(@event);
        }


        /// <summary>
        /// GET: /Events/Edit/id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Edit(int id)
        {
            var @event = _eventoDB.Events.Find(id);
            return View(@event);
        }


        /// <summary>
        /// POST: /Events/Edit/5 
        /// </summary>
        /// <param name="event"></param>
        /// <returns></returns>
        [HttpPost, Authorize]
        public ActionResult Edit(Event @event)
        {
            if (ModelState.IsValid)
            {
                _eventoDB.Entry(@event).State = EntityState.Modified;
                _eventoDB.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(@event);
        }


        /// <summary>
        /// GET: /Events/ 
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View(_eventoDB.Events.ToList());
        }


        /// <summary>
        /// Releases unmanaged resources and optionally releases managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            _eventoDB.Dispose();
            base.Dispose(disposing);
        }

		#endregion Methods 
    }
}